<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="模板图名称">
          <a-input placeholder="请输入模板图名称" v-decorator="['templateName', validatorRules.templateName]"/>
        </a-form-item>
        <a-form-item label="模板图" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1}"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="picUrl=='Has no pic url yet'" :src="getImgUrlView()" alt="模板图"
                 style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadLoading ? 'loading' : 'plus'"/>
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上线状态">
          <a-radio-group v-decorator="['onlineStatus',{'initialValue':0}]">
            <a-radio :value="0">未上线</a-radio>
            <a-radio :value="1">已上线</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea rows="3" v-decorator="['remark', {}]"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "FaceEffectsMergeTemplateModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        picUrl:null,
        uploadLoading: false,
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          templateName: {rules: [{required: true, message: '请模板图名称'}]}
        },
        url: {
          fileUpload: window._CONFIG['domianURL'] + "/rest/oss/tx/upload/merge",
          add: "/faceeffects/faceEffectsMergeTemplate/add",
          edit: "/faceeffects/faceEffectsMergeTemplate/edit",
        },
      }
    },
    created() {
    },
    methods: {
      //上传地址
      uploadAction: function () {
        return this.url.fileUpload;
      },
      //上传之前获取图片类型
      beforeUpload: function (file) {
        var fileType = file.type;
      },
      //获取图片地址
      getImgUrlView() {
        return this.model.templateUrl;
      },
      //上传图片回调
      handleChange(info) {
        this.picUrl = "";
        if (info.file.status === 'uploading') {
          this.uploadLoading = true;
          return
        }
        if (info.file.status === 'done') {
          var response = info.file.response;
          this.uploadLoading = false;
          if (response.code==200) {
            this.model.templateUrl = response.data.img_url;
            this.model.imgName = response.data.img_name;
            this.picUrl = "Has no pic url yet";
          } else {
            this.$message.warning(response.message);
          }
        }
      },
      //新增方法
      add() {
        this.picUrl = null;
        this.edit({templateUrl:''});
      },
      edit(record) {
        if(record.templateUrl!=""){
          this.picUrl = "Has no pic url yet";
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'templateName', 'onlineStatus', 'remark'))
          //时间格式化
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })


          }
        })
      },
      handleCancel() {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>